package com.jsbs.iam.ident.controller;

import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.param.AccountInfoDto;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.common.core.utils.StringUtils;
import com.jsbs.iam.ident.service.IdentityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/iam/ident/api")
public class IdentityApiController {

    @Autowired
    IdentityService identityService;

    /**
     * 用户中心自动创建账户接口
     * @param accountInfoDtos
     * @return Result
     */
    @PostMapping("/auto/account/create")
    public Result autoCreateAccount(@RequestBody List<AccountInfoDto> accountInfoDtos) {
        log.info("Route=>ApiIdentityController method=>autoCreateAccount 自动创建账户入参：{} ", Json.toJsonString(accountInfoDtos));
        try {
            return Result.success(identityService.accountCreate(accountInfoDtos));
        } catch (Exception e) {
            log.error("Route=>ApiIdentityController method=>autoCreateAccount 自动创建账户发生异常：", e);
            return Result.failWithMsg("自动创建账户异常");
        }
    }

    /**
     * 功能描述：用户中心将对应账户置为失效接口
     * @param userCodeList
     * @param request
     * @return Result
     */
    @PostMapping("/auto/account/fail")
    public Result autoFailAccount(HttpServletRequest request,@RequestBody List<String> userCodeList) {
        log.info("Route=>ApiIdentityController method=>autoFailAccount 自动将账户置为失效入参：{} ", Json.toJsonString(userCodeList));
        try {
            //当前登录人的所属归属租户
            String companyCode = (String) request.getSession().getAttribute("companyCode");
            //companyCpde先使用伪数据
            companyCode="000001";
            if (StringUtils.isBlank(companyCode)) {
                throw new RuntimeException("当前归属租户状态异常！");
            }
            identityService.accountFail(userCodeList,companyCode);
            return Result.success();
        } catch (Exception e) {
            log.error("Route=>ApiIdentityController method=>autoFailAccount 自动将账户置为失效发生异常：", e);
            return Result.failWithMsg("自动失效账户异常");
        }
    }

}


